package jinyilw.swing.visuals.Rcomp;

import jinyilw.common.Operator;
import jinyilw.swing.ScreenTools;
import jinyilw.swing.i18n.ICSetting;
import jinyilw.swing.icon.LazyScaleIcon;
import jinyilw.swing.rlaf.UITools;
import jinyilw.swing.rlaf.ui.RRootPaneUI;
import jinyilw.swing.rlaf.ui.RTitlePane;
import jinyilw.swing.visuals.comp.ScaleImageComp;

import javax.swing.JFrame;
import javax.swing.plaf.RootPaneUI;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GraphicsConfiguration;
import java.awt.Image;
import java.io.File;

public class RFrame extends JFrame
{
	private static final long serialVersionUID = -3572539935903245872L;

	private Dimension maxDimension;
	private Dimension defaultDimension;
	private RRootPaneUI rRootPaneUI = null;
	private RTitlePane rTitlePane = null;
	private final ScaleImageComp imageLogo = null;

	static
	{
		UITools.initRLookAndFeel();
	}

	public RFrame()
	{
		initGUI();
	}

	public RFrame(String title)
	{
		super(title);
		initGUI();
	}

	public RFrame(String title, GraphicsConfiguration gc)
	{
		super(title, gc);
		initGUI();
	}

	public RFrame(GraphicsConfiguration gc)
	{
		super(gc);
		initGUI();
	}

	private void initGUI()
	{
		RootPaneUI ui = getRootPane().getUI();
		if (ui instanceof RRootPaneUI)
		{
			rRootPaneUI = (RRootPaneUI) ui;
			rTitlePane = rRootPaneUI.getTitlePane();
		}
		maxDimension = ScreenTools.getDefaultScreenSize();
		int w = (int) (maxDimension.width * 0.6);
		int h = (int) (maxDimension.height * 0.6);
		defaultDimension = new Dimension(w, h);
	}

	public void setBGOpacity(int opacity)
	{
		if (rRootPaneUI != null)
			rRootPaneUI.setBrightness(opacity);
	}

	public void setCustomBGImage(Image img)
	{
		if (rRootPaneUI != null)
			rRootPaneUI.setCustomImage(img);
	}

	public void setRounded(boolean b)
	{
		if (rRootPaneUI != null)
			rRootPaneUI.setRounded(b);
	}

	public void setTitleColor(Color color)
	{
		if (rTitlePane != null)
			rTitlePane.setTitleColor(color);
	}

	public void setTitleHeight(int height)
	{
		if (rTitlePane != null)
			rTitlePane.setTitleHeight(height);
	}
	public void setTitleFont(Font font)
	{
		if (rTitlePane != null)
			rTitlePane.setTitleFont(font);
	}

	public void setTitle(ICSetting iSetting)
	{
		if (rTitlePane != null)
			rTitlePane.setTitle(iSetting);
	}

	public void cancelClosing()
	{
		if (rTitlePane != null)
			rTitlePane.cancelClosing();
	}

	public void addBeforeClosingOperator(Operator<?> operator)
	{
		if (rTitlePane != null)
			rTitlePane.addBeforeClosingOperator(operator);
	}

	public void addClosingOperator(Operator<?> operator)
	{
		if (rTitlePane != null)
			rTitlePane.addClosingOperator(operator);
	}

	public void addHideOperator(Operator<Object> operator)
	{
		if (rTitlePane != null)
			rTitlePane.addHideOperator(operator);
	}

	//	public void setIconImage(String path)
	//	{
	//		setIconImage(ResourceManager.getResImage(path));
	//	}

	public void setWindowSymbol(Component com)
	{
		if (rTitlePane != null)
			rTitlePane.setSymbol(com);
	}

	public void setWindowSymbol(Class<?> loadClass, String path)
	{
		setWindowSymbol(LazyScaleIcon.getIcon(loadClass, path));
	}

	public void setWindowSymbol(LazyScaleIcon scaleIcon)
	{
		setWindowSymbol(new ScaleImageComp(scaleIcon));
	}

	public void setChangeLogoOperator(Operator<File> operator)
	{
		if (rTitlePane != null)
			rTitlePane.setChangeLogoOperator(operator);
	}

	public void addActionComponent(Component comp)
	{
		if (rTitlePane != null)
			rTitlePane.addActionComponent(comp);
	}

	public void setFullScreenMode(boolean b)
	{
		if (rRootPaneUI != null)
			rRootPaneUI.setFullScreenMode(b);
	}

	public void setCustomMenuBar(Container m)
	{
		if (rRootPaneUI != null)
			rRootPaneUI.setCustomMenuBar(m);
	}

	public void setMaxDimension(Dimension d)
	{
		maxDimension = d;
	}

	public void setDefaultDimension(Dimension d)
	{
		defaultDimension = d;
	}

	public void setDefaultDimensionRatio(double wr, double hr)
	{
		int w = (int) (maxDimension.width * wr);
		int h = (int) (maxDimension.height * hr);
		defaultDimension = new Dimension(w, h);
	}

	public void defaultSize()
	{
		setSize(defaultDimension.width, defaultDimension.height);
	}

	public void maxSize()
	{
		setSize(maxDimension.width, maxDimension.height);
	}

	@Override
	public void setLocationRelativeTo(Component c)
	{
		super.setLocationRelativeTo(c);
		if (getY() < ScreenTools.getMinScreenY())
			setLocation(getX(), ScreenTools.getMinScreenY());
	}

	public void makeCustom()
	{
		UITools.uninstallTheme(this);
	}

	public void setSection(int n)
	{
		if (rRootPaneUI != null)
			rRootPaneUI.setContentSection(n);
	}
}
